package br.com.jw.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import br.com.jw.jdbc.Pool;
import br.com.jw.modelo.Permissoes;

public class PermissoesDao {
	Connection conn;
	private Pool pool;
	PreparedStatement stmt;
    boolean aux =false;
	public PermissoesDao(Pool pool) {
		this.pool = pool;
	}

	
	public boolean verificarConcederPermissao(int per_usu_id, int per_men_id) throws ClassNotFoundException, SQLException{
		String sql = "select * from permissoes where per_usu_id=? and per_men_id=?";
		
		conn = pool.getConnection();
		try{
			stmt =  conn.prepareStatement(sql);
			stmt.setInt(1, per_usu_id);
			stmt.setInt(2, per_men_id);
			ResultSet rs=stmt.executeQuery();
						
			if(rs.next()){
		    //
			}
			else{
			concederPermissoes(per_usu_id, per_men_id);
			}
			
			stmt.close();
			rs.close();
			return true;
			
		}finally{
			pool.liberarConexao(conn);
		}
	}
	public void concederPermissoes( int usu_id, int per_men_id) throws ClassNotFoundException, SQLException{
		String sql = "insert into permissoes (per_usu_id,per_men_id)values(?,?)";
		conn = pool.getConnection();
		
		try{
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, usu_id);
			stmt.setInt(2, per_men_id);
			
			stmt.execute();
			stmt.close();
			
		}finally{
			pool.liberarConexao(conn);
		}
	}
	public boolean excluirPermissoesUsuario(int usu_id,int per_men_id) throws ClassNotFoundException, SQLException{
		String sql="delete from permissoes where per_usu_id=? and per_men_id=?";
		conn = pool.getConnection();
		try{
			
		    stmt = conn.prepareStatement(sql);
			stmt.setInt(1, usu_id);
			stmt.setInt(2, per_men_id);
			stmt.execute();
			stmt.close();
			return true;
			
		}finally{
			pool.liberarConexao(conn);
		}
		
	}
	public List<Permissoes> listaPermissoesUsuario(int usu_id) throws ClassNotFoundException, SQLException {

		List<Permissoes> listaPermissoesUsuario = new ArrayList<Permissoes>();
		String sql = "select men_id, men_nome from menu m inner join permissoes p on p.per_men_id = m.men_id " +
				"inner join usuario u on u.usu_id=p.per_usu_id where usu_id=?";
		conn = pool.getConnection();

		try {
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, usu_id);
			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {
				listaPermissoesUsuario.add(new Permissoes(
						rs.getInt("men_id"),
						rs.getString("men_nome")));
			}
			stmt.close();
			rs.close();
			return listaPermissoesUsuario;

		} finally {
			pool.liberarConexao(conn);
		}
	}
	
	public List<Permissoes> getPermissoes(int usu_id) throws ClassNotFoundException, SQLException {

		List<Permissoes> listaPermissoes = new ArrayList<Permissoes>();
		String sql = "SELECT men_nome from menu m inner join permissoes p on p.per_men_id=m.men_id "
				+ "inner join usuario u on p.per_usu_id=u.usu_id where u.usu_id=?";

		conn = pool.getConnection();

		try {
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, usu_id);
			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {
				listaPermissoes.add(new Permissoes(rs.getString("men_nome")));
			}
			stmt.close();
			rs.close();
			return listaPermissoes;

		} finally {
			pool.liberarConexao(conn);
		}
	}
}